﻿using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Input;
using Central.PumpService;

namespace Central
{
    public partial class MainWindow
    {
        private readonly CentralTerminalServiceClient _proxy;
        private readonly CentralTerminalCallback _callback;

        public MainWindow()
        {
            InitializeComponent();

            _callback = new CentralTerminalCallback();
            var instanceContext = new InstanceContext(_callback);
            _proxy = new CentralTerminalServiceClient(instanceContext);
            _proxy.RegisterCentralTerminal(2000);

            var pumpStations = _proxy.GetPumpStations();
            ListPumpStations.ItemsSource = pumpStations;
        }

        private void ListPumpStations_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ListPumpStations.SelectedItem != null)
            {
                var pumpStationDTO = (PumpStationDTO) ListPumpStations.SelectedItem;
                new PumpOverview(this, _proxy, pumpStationDTO, _callback).Show();
                Hide();
            }
        }

        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            _proxy.UnregisterCentralTerminal(2000);
        }
    }
}
